<div id="dashboard" v-cloak>
  <div class="container pb-5 px-3 px-xl-0">

    <div purpose="renewal-notification" class="d-flex flex-row justify-content-start align-items-center" v-if="subscriptionHasBeenRecentlyRenewed">
      <div class="pr-4">
        <img alt="A green checkmark" style="height: 24px;" src="/images/icon-green-check-circle-24x24@2x.png">
      </div>
      <div class="d-flex flex-column">
        <p>Your Fleet Premium subscription has successfully renewed.</p>
        <p>To continue using Fleet, please <strong>attach your new license key</strong> to your Fleet instance.</p>
      </div>
    </div>

    <div purpose="subscription-expiring-notification" v-if="subscriptionExpiresSoon" class="d-flex flex-row justify-content-start align-items-center">
      <div class="pr-4">
        <img alt="Information icon" style="height: 24px;" src="/images/info-16x16@2x.png">
      </div>
      <div class="d-flex flex-column">
        <p>Your Fleet Premium subscription will automatically renew on <js-timestamp :at="thisSubscription.nextBillingAt" always-show-year format="billing"></js-timestamp>.</p>
        <p>Please <a href="/contact">contact us</a> to change your number of devices, or to cancel your subscription.</p>
      </div>
    </div>

    <div class="pb-4">
      <h3>Your license</h3>
      <div purpose="license-card" class="card card-body">
        <div class="row">
          <div class="col-12 col-md-6 col-lg-2">
            <strong>Item</strong>
            <p>Fleet Premium (self-hosted)</p>
          </div>
          <div class="col-12 col-md-6 col-lg-2 pt-3 pt-md-0">
            <strong>Cost</strong>
            <p>${{thisSubscription.subscriptionPrice.toFixed(2)}}/year</p>
          </div>
          <div class="col-12 col-md-6 col-lg-2 pt-3 pt-lg-0">
            <strong>No. of devices</strong>
            <p>{{thisSubscription.numberOfHosts}}</p>
          </div>
          <div class="col-12 col-md-6 col-lg-2 pt-3 pt-lg-0">
            <strong>Expires</strong>
            <p><js-timestamp :at="thisSubscription.nextBillingAt" always-show-year format="billing"></js-timestamp></p>
          </div>
          <div class="col-12 col-lg-4 pt-3 pt-lg-0">
            <strong>License key</strong><img class="d-inline-block ml-2" style="height: 14px; cursor: pointer;" src="/images/icon-copy-14x14@2x.png" alt="click here to copy your license key" @click="clickCopyLicenseKey()"><span purpose="copied-notification">License key copied!</span>
            <p><span purpose="license-key" @click="clickExpandLicenseKey()">{{thisSubscription.fleetLicenseKey}}</span></p>
          </div>
        </div>
      </div>
    </div>

    <div class="row pb-4">
      <div class="col-12 col-lg-6 pb-4 pb-lg-0">
      <h3 class="pt-2 pt-sm-3">Your details</h3>
        <div purpose="details-card" class="card card-body justify-content-start">
          <div class="row">
            <div class="col-sm-4 col-12 pb-2 pb-sm-0">Organization:</div>
            <div class="col-sm-8 col-12 text-left text-sm-right">
              <strong>{{me.organization}}</strong><img purpose="edit-button" src="/images/icon-pencil-12x12@2x.png" alt="A pencil icon indicating that this information can be editted" @click="clickEditButton()">
            </div>
          </div>
          <hr/>
          <div class="row">
            <div class="col-sm-3 col-12 pb-2 pb-sm-0">Name:</div>
            <div class="col-sm-9 col-12 text-left text-sm-right">
              <strong>{{me.firstName}} {{me.lastName}}</strong><img purpose="edit-button" src="/images/icon-pencil-12x12@2x.png" alt="A pencil icon indicating that this information can be editted" @click="clickEditButton()">
            </div>
          </div>
          <hr/>
          <div class="row">
            <div class="col-sm-3 col-12 pb-2 pb-sm-0">Email:</div>
            <div class="col-sm-9 col-12 text-left text-sm-right">
              <strong :class="[me.emailStatus === 'unconfirmed' || me.emailStatus === 'change-requested' ? 'text-muted' : '']">{{me.emailChangeCandidate ? me.emailChangeCandidate : me.emailAddress}}</strong><img purpose="edit-button" src="/images/icon-pencil-12x12@2x.png" alt="A pencil icon indicating that this information can be editted" @click="clickEditButton()">
            </div>
          </div>
          <hr/>
          <div class="row">
            <div class="col-sm-4 col-12 pb-2 pb-sm-0">Password:</div>
            <div class="col-sm-8 col-12 text-left text-sm-right">
              <strong>••••••••</strong><img purpose="edit-button" src="/images/icon-pencil-12x12@2x.png" alt="A pencil icon indicating that this information can be editted" @click="clickChangePassword()">
            </div>
          </div>
        </div>
      </div>

      <div class="col-12 col-lg-6">
        <h3 class="pt-2 pt-sm-3">Billing and payment</h3>
        <div purpose="billing-card" class="card card-body justify-content-start" v-if="!subscriptionIsExpired">
          <div class="row pb-3 mx-0">
            <div style="max-width: 16px;" class="col-1 px-0"><img style="margin-top: 5px; height: 16px; width: 16px;" src="/images/icon-calendar-32x32@2x.png" alt="A calendar icon"></div>
            <div class="col pl-3">
              <p>{{thisSubscription.numberOfHosts}} devices @ ${{(thisSubscription.subscriptionPrice / thisSubscription.numberOfHosts / 12).toFixed(2)}}/device/month</p>
              <p>Billed annually at ${{thisSubscription.subscriptionPrice.toFixed(2)}}/yr</p>
              <p v-if="willSubscriptionRenew">Next payment on <js-timestamp :at="thisSubscription.nextBillingAt" always-show-year format="billing"></js-timestamp></p>
              <p v-else>Your subscription will expire on <js-timestamp :at="thisSubscription.nextBillingAt" always-show-year format="billing"></js-timestamp></p>
            </div>
          </div>
            <div purpose="contact">
            <div class="row">
              <div style="max-width: 32px;" class="col-1 pr-0">
                <img style="display: inline-block; height: 16px; width: 16px; margin-top: -3px;" src="/images/info-16x16@2x.png" alt="An icon indicating that this section has important information">
              </div>
              <div class="col ml-1 pl-1 small">
                <p class="small"><a href="/customers/update-subscription">Click here</a> to change your number of devices, or to cancel your subscription.</p>
              </div>
            </div>
          </div>
        </div>
        <div purpose="billing-card" class="card card-body justify-content-start" v-else>
          <div class="row">
            <div style="max-width: 32px;" class="col-1 pr-0">
              <img style="display: inline-block; height: 16px; width: 16px; " src="/images/icon-info-grey-16x16@2x.png" alt="An icon indicating that this section has important information">
            </div>
            <div class="col ml-1 pl-1 small">
              <p class="small">Your subscription ended on <js-timestamp :at="thisSubscription.nextBillingAt" always-show-year format="billing"></js-timestamp></p>
            </div>
          </div>
        </div>
      </div>

    </div>
    <div class="d-flex flex-md-row flex-column pt-3 align-items-center justify-content-md-start justify-content-center">
        <a class="btn btn-primary btn-sm btn-md-block mb-3 mb-md-0" purpose="deploy-button" href="/docs/deploying/introduction">
          How to deploy Fleet
        </a>
        <animated-arrow-button href="/support" target="_blank">Support
        </animated-arrow-button>
    </div>

  </div>

  <modal v-if="modal === 'update-profile'" @close="closeModal()" data-backdrop="false" v-cloak purpose="modal">
    <div class="container" purpose="customer-portal-form">
      <div class="modal-header">
        <h3 class="modal-title text-left">Edit your details?</h3>
      </div>
      <ajax-form action="updateProfile" class="customers-update-profile" :syncing.sync="syncing" :cloud-error.sync="cloudError" :form-data="formData" :form-rules="formRules" :form-errors.sync="formErrors" @submitted="submittedUpdateProfileForm()">
        <div class="modal-body">
          <div class="form-group">
            <label for="organization">Organization</label>
            <input class="form-control" id="organization" type="text" :class="[formErrors.organization ? 'is-invalid' : '']" v-model.trim="formData.organization" autocomplete="">
            <div class="invalid-feedback" v-if="formErrors.organization">Please enter the name of your organization.</div>
          </div>
          <div class="row">
            <div class="col-6 pr-2">
              <div class="form-group">
                <label for="first-name">First name</label>
                <input class="form-control" id="first-name" type="text"  :class="[formErrors.firstName ? 'is-invalid' : '']" v-model.trim="formData.firstName" autocomplete="first-name">
                <div class="invalid-feedback" v-if="formErrors.firstName">Please enter your first name.</div>
              </div>
            </div>
            <div class="col-6 pl-2">
              <div class="form-group">
                <label for="last-name">Last name</label>
                <input class="form-control" id="last-name" type="text"  :class="[formErrors.lastName ? 'is-invalid' : '']" v-model.trim="formData.lastName" autocomplete="last-name">
                <div class="invalid-feedback" v-if="formErrors.lastName">Please enter your last name.</div>
              </div>
            </div>
          </div>
          <div class="form-group">
            <label for="email-address">Email</label>
            <input class="form-control" id="email-address" type="email"  :class="[formErrors.emailAddress ? 'is-invalid' : '']" v-model.trim="formData.emailAddress" placeholder="sturgeon@example.com" autocomplete="email">
            <div class="invalid-feedback" v-if="formErrors.emailAddress" focus-first>Please enter a valid email address.</div>
          </div>
        </div>
        <cloud-error class="mt-0" v-if="cloudError === 'emailAlreadyInUse'">That email is already linked to another Fleet account.</cloud-error>
        <cloud-error class="mt-0" v-else-if="cloudError"></cloud-error>
        <div class="border-0 justify-content-center">
          <ajax-button purpose="submit-button" spinner="true" type="submit" :syncing="syncing" class="btn btn-sm btn-block btn-info" focus-first>Save</ajax-button>
        </div>
      </ajax-form>
    </div>
  </modal>

  <modal v-if="modal === 'update-password'" @close="closeModal()" data-backdrop="false" v-cloak purpose="modal">
    <div class="container" purpose="customer-portal-form">
      <div class="modal-header">
        <h3 class="modal-title text-left">Change your password?</h3>
      </div>
      <ajax-form action="updatePassword" :syncing.sync="syncing" :cloud-error.sync="cloudError" :form-data="formData" :form-rules="formRules" :form-errors.sync="formErrors" @submitted="submittedUpdatePasswordForm()">
        <div class="modal-body">
          <div class="form-group">
            <label for="oldPassword">Current password</label>
            <input class="form-control" type="password" id="oldPassword" :class="[formErrors.oldPassword ? 'is-invalid' : '']" v-model.trim="formData.oldPassword" autocomplete="password">
            <div class="invalid-feedback" v-if="formErrors.oldPassword">Please enter your current password.</div>
          </div>
          <div class="form-group">
            <label for="password">New password</label>
            <input class="form-control" type="password" id="password" :class="[formErrors.newPassword ? 'is-invalid' : '']" v-model.trim="formData.newPassword">
            <div class="invalid-feedback" v-if="formErrors.newPassword === 'minLength'">Password too short!</div>
            <div class="invalid-feedback" v-else-if="formErrors.newPassword">Please enter your new password.</div>
            <p class="mt-2"> Minimum length is 8 characters</p>
          </div>
          <cloud-error v-if="cloudError === 'badPassword'">Incorrect current password.</cloud-error>
          <cloud-error v-else-if="cloudError"></cloud-error>
        </div>
        <div class="border-0 justify-content-center">
          <ajax-button purpose="submit-button" spinner="true" type="submit" :syncing="syncing" class="btn btn-sm btn-block btn-info" focus-first>Save</ajax-button>
        </div>
      </ajax-form>
    </div>
  </modal>

  <modal v-if="modal === 'update-billing-card'" data-backdrop="false" @close="closeModal()" v-cloak purpose="modal">
    <div class="container" purpose="customer-portal-form">
      <div class="modal-header text-left">
        <h3 class="modal-title">Edit card?</h3>
        <p class="muted">Currently using: {{me.billingCardBrand}} ending in {{me.billingCardLast4}}</p>
      </div>
      <ajax-form :handle-submitting="handleSubmittingUpdateBillingCard" :syncing.sync="syncing" :cloud-error.sync="cloudError" :form-data="formData" :form-rules="formRules" :form-errors.sync="formErrors" @submitted="submittedUpdateBillingCard()">
        <div class="modal-body">
          <stripe-card-element :stripe-publishable-key="stripePublishableKey" :is-errored.sync="formErrors.newPaymentSource" v-model="formData.newPaymentSource" key="billing-card" ref="billingcardref"></stripe-card-element>
          <cloud-error v-if="cloudError"></cloud-error>
        </div>
        <div class="pt-3">
          <ajax-button purpose="submit-button" spinner="true" :syncing="syncing" class="btn btn-sm btn-block btn-info" focus-first>Save card</ajax-button>
        </div>
      </ajax-form>
    </div>
  </modal>

  <modal v-if="modal === 'remove-billing-card'" @close="closeModal()" v-cloak>
    <div class="container">
      <h3>Remove Card Info?</h3>
      <ajax-form action="updateBillingCard" :syncing.sync="syncing" :cloud-error.sync="cloudError" :form-data="formData" :form-rules="formRules" :form-errors.sync="formErrors" @submitted="submittedRemoveCardForm()">
        <div class="modal-body text-center">
          <p>Are you sure you want to remove your {{me.billingCardBrand}} ending in <strong>{{me.billingCardLast4}}</strong>?</p>
          <p class="text-muted mb-0">This may restrict your access to paid features.</p>
          <cloud-error v-if="cloudError"></cloud-error>
        </div>
        <div class="border-0 justify-content-center">
          <ajax-button purpose="submit-button" spinner="true" type="submit" :syncing="syncing" class="btn btn-sm btn-block btn-primary">Remove</ajax-button>
        </div>
      </ajax-form>
    </div>
  </modal>

</div>
<%- /* Expose server-rendered data as window.SAILS_LOCALS :: */ exposeLocalsToBrowser() %>
